﻿using UnityEngine;
using System.Collections;

public class support : MonoBehaviour 
{
    public GameObject gameBullet;
	private Transform center;
	public int orbitDirection  = 1;
	private Vector3 axis = new Vector3 (0,0,-1);
	public float radius = 2.0f;
	public float radiusSpeed = 0.5f;
	public float rotationSpeed = 80.0f;

    private float offSetX = 270;
    private float offSetY = 0;
    private float offSetZ = 0;
	private float mTimer = 0.0f;
	
    public float FireRate = .75f;
    private float FireTimer = 0;
	private int firerateMultiplier;
	
	void Start ()
	{

        center = transform.parent.transform;
		transform.position = (transform.position - center.position).normalized * radius + center.position;
	}
 
	void Update ()
	{
		FireRate = 2.75f-(float)PlayerPrefs.GetInt("Support")/50f;
		transform.RotateAround (center.position, axis*orbitDirection, rotationSpeed * Time.deltaTime);
		var desiredPosition = (transform.position - center.position).normalized * radius + center.position;
		transform.position = Vector3.MoveTowards (transform.position, desiredPosition, Time.deltaTime * radiusSpeed);
        transform.rotation = Quaternion.Euler(new Vector3(270, 0, 0));
        //Attack();
        if (FireTimer > FireRate)
        {
            ShootBullets();
            FireTimer = 0;
        }
        FireTimer += Time.deltaTime;
	 	findAngle();
	}

	float findAngle ()
	{
		float angle = Mathf.Atan2 (center.position.y - transform.position.y, center.position.x - transform.position.x) * 180 / Mathf.PI;
//		Debug.Log(angle);
		return angle;
	}
    void ShootBullets()
    {
        Weapon bullet = ((GameObject)Instantiate(gameBullet, transform.position, Quaternion.Euler(new Vector3(offSetX, offSetY, offSetZ)))).GetComponent<Weapon>();//0,90,270
        bullet.currWeapon = Weapon.weaponState.Bullet;
        Physics.IgnoreCollision(collider, bullet.collider, true);
        bullet.fireDirection = Vector3.Cross(new Vector3(offSetX, offSetY, offSetZ), Vector3.up).normalized;
    }

    public void OnCollisionEnter(Collision col)
    {
		if(col.gameObject.tag == "LevelBounds")
		{
			orbitDirection*=-1;
		}
	}

}




